<template>
  <div class="member-order">
    <!-- 标签页 -->
    <!--
      name:  提供给v-model用的
      label: 提供tab的名字，必传
      插槽是用来显示对应标签页的内容
      -->

    <!-- 遍历生成多个标签页 -->
    <xtx-tabs v-model="activeName" @tab-click="handleTabClick">
      <xtx-tabs-panel v-for="item in orderStatus" :key="item.name" :name="item.name" :label="item.label" />
    </xtx-tabs>
    <!-- 显示的内容 -->
    <div class="order-list">
      <div class="order-item" v-for="item in orderList" :key="item.id">
        <div class="head">
          <span>下单时间：{{ item.createTime }}</span>
          <span>订单编号：{{ item.id }}</span>
          <span class="down-time">
            <i class="iconfont icon-down-time"></i>
            <b>付款截止：{{ item.payLatestTime }}</b>
          </span>
        </div>
        <div class="body">
          <div class="column goods">
            <ul>
              <li v-for="sku in item.skus" :key="sku.id">
                <a class="image" href="javascript:;">
                  <img :src="sku.image" alt="image" />
                </a>
                <div class="info">
                  <p class="name ellipsis-2">{{ sku.name }}</p>
                  <p class="attr ellipsis">
                    <span v-for="spec, index in sku.properties" :key="index">
                      {{ spec.propertyMainName }}:{{ spec.propertyValueName }}
                    </span>
                  </p>
                </div>
                <div class="price">¥{{ sku.curPrice?.toFixed(2) }}</div>
                <div class="count">x{{ sku.quantity }}</div>
              </li>
            </ul>
          </div>
          <div class="column state">
            <p>{{ tabName }}</p>
          </div>
          <div class="column amount">
            <p class="red">¥{{ item.payMoney?.toFixed(2) }}</p>
            <p>（含运费：¥10.00）</p>
            <p>在线支付</p>
          </div>
          <div class="column action">
            <XtxButton type="primary" size="small">立即付款</XtxButton>
            <p><a href="javascript:;">查看详情</a></p>
            <p><a href="javascript:;">取消订单</a></p>
          </div>
        </div>
      </div>
    </div>

  </div>
</template>
<script>
import { ref, watch } from 'vue'
import { getOrderListAPI } from '@/api/order'
export default {
  name: 'MemberOrder',
  setup () {
    const activeName = ref('all')
    // 当前订单的状态，默认值是0，表示全部订单
    const orderState = ref(0)
    // 订单状态数组
    const orderStatus = [
      { name: 'all', label: '全部订单' },
      { name: 'unpay', label: '待付款' },
      { name: 'deliver', label: '待发货' },
      { name: 'receive', label: '待收货' },
      { name: 'comment', label: '待评价' },
      { name: 'complete', label: '已完成' },
      { name: 'cancel', label: '已取消' }
    ]
    // 订单列表
    const orderList = ref([])
    getOrderList()
    // 获取订单列表
    async function getOrderList () {
      const queryObj = {
        page: 1,
        pageSize: 5,
        orderState: orderState.value
      }
      const resp = await getOrderListAPI(queryObj)
      orderList.value = resp.result.items
    }

    // 点击切换数据
    // 侦听 orderState 的变化
    watch(orderState, () => {
      // 重新获取新的数据
      getOrderList()
    })

    const tabName = ref('全部订单')
    const handleTabClick = ({ index, tab }) => {
      orderState.value = index
      tabName.value = tab.props.label
    }
    return {
      activeName,
      orderStatus,
      handleTabClick,
      orderState,
      orderList,
      tabName
    }
  }
}
</script>
<style scoped lang="less">
.order-list {
  background: #fff;
  padding: 20px;
}

.order-item {
  margin-bottom: 20px;
  border: 1px solid #f5f5f5;

  .head {
    height: 50px;
    line-height: 50px;
    background: #f5f5f5;
    padding: 0 20px;
    overflow: hidden;

    span {
      margin-right: 20px;

      &.down-time {
        margin-right: 0;
        float: right;

        i {
          vertical-align: middle;
          margin-right: 3px;
        }

        b {
          vertical-align: middle;
          font-weight: normal;
        }
      }
    }

    .del {
      margin-right: 0;
      float: right;
      color: #999;
    }
  }

  .body {
    display: flex;
    align-items: stretch;

    .column {
      border-left: 1px solid #f5f5f5;
      text-align: center;
      padding: 20px;

      >p {
        padding-top: 10px;
      }

      &:first-child {
        border-left: none;
      }

      &.goods {
        flex: 1;
        padding: 0;
        align-self: center;

        ul {
          li {
            border-bottom: 1px solid #f5f5f5;
            padding: 10px;
            display: flex;

            &:last-child {
              border-bottom: none;
            }

            .image {
              width: 70px;
              height: 70px;
              border: 1px solid #f5f5f5;
            }

            .info {
              width: 220px;
              text-align: left;
              padding: 0 10px;

              p {
                margin-bottom: 5px;

                &.name {
                  height: 38px;
                }

                &.attr {
                  color: #999;
                  font-size: 12px;

                  span {
                    margin-right: 5px;
                  }
                }
              }
            }

            .price {
              width: 100px;
            }

            .count {
              width: 80px;
            }
          }
        }
      }

      &.state {
        width: 120px;

        .green {
          color: @xtxColor;
        }
      }

      &.amount {
        width: 200px;

        .red {
          color: @priceColor;
        }
      }

      &.action {
        width: 140px;

        a {
          display: block;

          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
}
</style>
